<template>
  <component
    :is="tag"
    :class="buttonClasses"
    :disabled="disabled || loading"
    :type="buttonType"
    :href="href"
    :target="target"
    :to="to"
    @click="handleClick"
  >
    <span v-if="loading" class="button-loading">
      <div class="loading-spinner"></div>
    </span>
    
    <span v-if="icon && iconPosition === 'left'" class="button-icon button-icon--left">
      <component :is="icon" />
    </span>
    
    <span class="button-content">
      <slot />
    </span>
    
    <span v-if="icon && iconPosition === 'right'" class="button-icon button-icon--right">
      <component :is="icon" />
    </span>
    
    <span v-if="badge" class="button-badge">{{ badge }}</span>
  </component>
</template>

<script setup lang="ts">
import { computed } from 'vue'

// Props定义
interface Props {
  // 按钮类型
  variant?: 'primary' | 'secondary' | 'success' | 'warning' | 'danger' | 'text' | 'link'
  // 按钮尺寸
  size?: 'small' | 'medium' | 'large'
  // 是否禁用
  disabled?: boolean
  // 是否加载中
  loading?: boolean
  // 是否块级元素
  block?: boolean
  // 是否圆形
  round?: boolean
  // 图标
  icon?: any
  // 图标位置
  iconPosition?: 'left' | 'right'
  // 徽章
  badge?: string | number
  // HTML标签
  tag?: 'button' | 'a' | 'router-link'
  // 按钮类型（仅button标签）
  type?: 'button' | 'submit' | 'reset'
  // 链接地址（仅a标签）
  href?: string
  // 链接目标（仅a标签）
  target?: '_blank' | '_self' | '_parent' | '_top'
  // 路由地址（仅router-link）
  to?: string | object
}

// Emits定义
interface Emits {
  (e: 'click', event: MouseEvent): void
}

const props = withDefaults(defineProps<Props>(), {
  variant: 'primary',
  size: 'medium',
  disabled: false,
  loading: false,
  block: false,
  round: false,
  iconPosition: 'left',
  tag: 'button',
  type: 'button'
})

const emit = defineEmits<Emits>()

// 计算属性
const buttonClasses = computed(() => {
  const classes = ['apple-button']
  
  // 变体样式
  classes.push(`apple-button--${props.variant}`)
  
  // 尺寸样式
  if (props.size !== 'medium') {
    classes.push(`apple-button--${props.size}`)
  }
  
  // 状态样式
  if (props.disabled) {
    classes.push('apple-button--disabled')
  }
  
  if (props.loading) {
    classes.push('apple-button--loading')
  }
  
  if (props.block) {
    classes.push('apple-button--block')
  }
  
  if (props.round) {
    classes.push('apple-button--round')
  }
  
  if (props.icon && !props.$slots.default) {
    classes.push('apple-button--icon-only')
  }
  
  return classes
})

const buttonType = computed(() => {
  return props.tag === 'button' ? props.type : undefined
})

// 事件处理
const handleClick = (event: MouseEvent) => {
  if (!props.disabled && !props.loading) {
    emit('click', event)
  }
}
</script>

<style scoped>
.apple-button {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--spacing-xs);
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius-medium);
  font-family: inherit;
  font-size: 14px;
  font-weight: 500;
  line-height: 1.4;
  text-decoration: none;
  text-align: center;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  outline: none;
  transition: all var(--duration-fast) var(--ease-out);
  overflow: hidden;

  &:focus-visible {
    box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.3);
  }

  &:active {
    transform: scale(0.98);
  }
}

/* 按钮变体 */
.apple-button--primary {
  background-color: var(--color-primary);
  color: white;

  &:hover:not(.apple-button--disabled):not(.apple-button--loading) {
    background-color: var(--apple-blue-dark);
  }
}

.apple-button--secondary {
  background-color: var(--bg-secondary);
  color: var(--text-primary);
  border: 1px solid var(--border-primary);

  &:hover:not(.apple-button--disabled):not(.apple-button--loading) {
    background-color: var(--bg-tertiary);
    border-color: var(--apple-gray-3);
  }
}

.apple-button--success {
  background-color: var(--color-success);
  color: white;

  &:hover:not(.apple-button--disabled):not(.apple-button--loading) {
    background-color: var(--apple-green-dark);
  }
}

.apple-button--warning {
  background-color: var(--color-warning);
  color: white;

  &:hover:not(.apple-button--disabled):not(.apple-button--loading) {
    background-color: var(--apple-orange-dark);
  }
}

.apple-button--danger {
  background-color: var(--color-danger);
  color: white;

  &:hover:not(.apple-button--disabled):not(.apple-button--loading) {
    background-color: var(--apple-red-dark);
  }
}

.apple-button--text {
  background-color: transparent;
  color: var(--color-primary);
  padding: 4px 8px;

  &:hover:not(.apple-button--disabled):not(.apple-button--loading) {
    background-color: rgba(0, 122, 255, 0.1);
  }
}

.apple-button--link {
  background-color: transparent;
  color: var(--color-primary);
  padding: 0;
  border-radius: 0;
  text-decoration: underline;

  &:hover:not(.apple-button--disabled):not(.apple-button--loading) {
    text-decoration: none;
  }
}

/* 按钮尺寸 */
.apple-button--small {
  padding: 4px 12px;
  font-size: 12px;
  border-radius: var(--radius-small);
}

.apple-button--large {
  padding: 12px 24px;
  font-size: 16px;
  border-radius: var(--radius-large);
}

/* 按钮状态 */
.apple-button--disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none !important;
}

.apple-button--loading {
  cursor: wait;
  transform: none !important;
}

.apple-button--loading .button-content {
  opacity: 0.6;
}

/* 布局变体 */
.apple-button--block {
  width: 100%;
  display: flex;
}

.apple-button--round {
  border-radius: 50px;
}

.apple-button--icon-only {
  padding: 8px;
  aspect-ratio: 1;
}

.apple-button--icon-only.apple-button--small {
  padding: 4px;
}

.apple-button--icon-only.apple-button--large {
  padding: 12px;
}

/* 按钮内容 */
.button-loading {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.button-content {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  transition: opacity var(--duration-fast) var(--ease-out);
}

.button-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.button-icon--left {
  margin-right: var(--spacing-xs);
}

.button-icon--right {
  margin-left: var(--spacing-xs);
}

.button-badge {
  position: absolute;
  top: -6px;
  right: -6px;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  background-color: var(--color-danger);
  color: white;
  font-size: 10px;
  font-weight: 600;
  line-height: 18px;
  text-align: center;
  border-radius: 9px;
  transform: scale(0.9);
}

.loading-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top: 2px solid currentColor;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .apple-button {
    min-height: 44px; /* Apple推荐的最小触摸目标尺寸 */
  }
  
  .apple-button--small {
    min-height: 36px;
  }
  
  .apple-button--large {
    min-height: 52px;
  }
}

/* 暗色模式适配 */
@media (prefers-color-scheme: dark) {
  .apple-button--secondary {
    background-color: var(--bg-tertiary);
    border-color: var(--border-secondary);
    
    &:hover:not(.apple-button--disabled):not(.apple-button--loading) {
      background-color: var(--apple-gray-4);
    }
  }
}

/* 高对比度模式 */
@media (prefers-contrast: high) {
  .apple-button {
    border: 2px solid;
  }
  
  .apple-button--primary {
    border-color: var(--apple-blue-dark);
  }
  
  .apple-button--secondary {
    border-color: var(--text-primary);
  }
}

/* 减少动画模式 */
@media (prefers-reduced-motion: reduce) {
  .apple-button {
    transition: none;
  }
  
  .apple-button:active {
    transform: none;
  }
  
  .loading-spinner {
    animation: none;
  }
}
</style>